-
Notifications
You must be signed in to change notification settings - Fork 67
[ZH] Fix loop index comparison warnings #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
14f7171
to
8aa4016
Compare
If you are going to change int to an unsigned type then I think std::size_t would be more appropriate and in line with standard conventions. Where range loops can be used then they should be preferred |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fairly safe, but I suggest to give it a test for mismatch, especially with a focus on USA Countermeasures.
size_t
is identical to std::size_t
.
@@ -413,7 +413,7 @@ static void findHighFileNumber( AsciiString filename, void *userData ) | |||
|
|||
// strip off the extension at the end of the filename | |||
AsciiString nameOnly = filename; | |||
for( Int count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ ) | |||
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this will call strlen on every iteration...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah i think the VC6 version may do that but modern MSVC would likely compile it out as a constant.
@@ -579,7 +579,7 @@ Bool MapCache::loadUserMaps() | |||
{ | |||
AsciiString endingStr; | |||
AsciiString fname = s+1; | |||
for (Int i=0; i<strlen(mapExtension); ++i) | |||
for (size_t i=0; i<strlen(mapExtension); ++i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another strlen in condition...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah... i didn't want to change too much.
@@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley() | |||
Object *obj = getObject(); | |||
|
|||
Real volleySize = (Real)data->m_volleySize; | |||
for( int i = 0; i < data->m_volleySize; i++ ) | |||
for( UnsignedInt i = 0; i < data->m_volleySize; i++ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope this will produce the same float value below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory it should as the raw binary values of positive signed int and unsigned int values are the same up to 2.7 billion at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting how it changes that much when the index is only used in one place.
8aa4016
to
f4c5b85
Compare
Updated this with the inclusion of a cast that was missing in one of the loops. |
f4c5b85
to
c0eff59
Compare
Just rebased off recent main after the other warning fixes were merged. |
Just ran Golden Replay 1 against this with the VC6 build and it did not mismatch. So I think the changes are fairly safe, i can replicate the changes to generals if people are happy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unassemblize shows significant assembler changes in the following 4 functions. I suggest to focus mismatch test on functionality related to these.
ScriptActions::doMoveTeamTowardsNearest
DeliverPayloadNugget::create
W3DLaserDraw::W3DLaserDraw
CountermeasuresBehaviour::launchVolley
@@ -5296,7 +5296,7 @@ void ScriptActions::doMoveTeamTowardsNearest( const AsciiString& teamName, const | |||
{ | |||
Real closestDist; | |||
Real dist; | |||
for( Int typeIndex = 0; typeIndex < objectTypes->getListSize(); typeIndex++ ) | |||
for( size_t typeIndex = 0; typeIndex < objectTypes->getListSize(); typeIndex++ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i don't get that, if anything i would have through that most code changes would be reduction in code due to not needing to perform some casts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks as if the function call has changed, but perhaps it is just an error in the tool.
@@ -322,7 +322,7 @@ class DeliverPayloadNugget : public ObjectCreationNugget | |||
} | |||
|
|||
Object *firstTransport = NULL; | |||
for( Int formationIndex = 0; formationIndex < m_formationSize; formationIndex++ ) | |||
for( UnsignedInt formationIndex = 0; formationIndex < m_formationSize; formationIndex++ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -156,7 +156,7 @@ W3DLaserDraw::W3DLaserDraw( Thing *thing, const ModuleData* moduleData ) : | |||
//Allocate an array of lines equal to the number of beams * segments | |||
m_line3D = NEW SegmentedLineClass *[ data->m_numBeams * data->m_segments ]; | |||
|
|||
for( int segment = 0; segment < data->m_segments; segment++ ) | |||
for( UnsignedInt segment = 0; segment < data->m_segments; segment++ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes 55% of the assembler in W3DLaserDraw::W3DLaserDraw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats interesting, you would think it would just be a few casts that got changed since segment is only used it one place within the function
@@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley() | |||
Object *obj = getObject(); | |||
|
|||
Real volleySize = (Real)data->m_volleySize; | |||
for( int i = 0; i < data->m_volleySize; i++ ) | |||
for( UnsignedInt i = 0; i < data->m_volleySize; i++ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.
This PR fixes loop index comparison warnings by matching the data type of the loop index to match the comparison values data type.
TODO: